<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>编辑</title>
    <style type="text/css">
        #msg{
            color: red;
        }
    </style>
    <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.3.js"></script>
    <script>
        var userNameFlag = true;
        var passwordFlag = true;
        function toSubmit(){
            if(userNameFlag&&passwordFlag){
                var name = $("#userName").val();
                var id = $("#id").val();
                $.ajax({
                    type : "POST",
                    url : "/scvs/student/check",
                    data : {
                        userName:name,
                        id:id
                    },
                    dataType: "json",
                    success : function(data) {
                        console.log(data);
                        if(data){
                            $("#studentForm").submit();
                        }else{
                            $("#msg").text("用户名已存在，请重新输入");
                        }
                    }
                });
            }else{
                $("#msg").val("还有信息未输入");
                return;
            }
        }
        function checkUserName(obj){
            if(obj.value.trim() == ""){
                userNameFlag = false;
                $("#msg").text("请输入用户名");
            }else{
                userNameFlag = true;
            }
        }
        function checkPassword(obj){
            if(obj.value.trim() == ""){
                passwordFlag = false;
                $("#msg").text("请输入密码");
            }else{
                passwordFlag = true;
            }
        }
    </script>
</head>
<body>
<h1 align="center">编辑学生</h1>
<form id="studentForm" action="/scvs/student/update" method="post">
    <input type="hidden" id="id" name="student.id" value="${student.id}"/>
    <table align="center">
        <tr>
            <td>登录名</td>
            <td><input type="text" onblur="checkUserName(this)" id="userName" value="${student.userName}" name="student.userName"/></td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" value="${student.realName}" name="student.realName"/></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="text" onblur="checkPassword(this)" value="${student.password}" name="student.password"/></td>
        </tr>
        <tr>
            <td>年级</td>
            <td><input type="text" value="${student.grade}" name="student.grade"/></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><span id="msg"></span></td>
        </tr>
        <tr>
            <td clospan="2"><input type="button" onclick="toSubmit()" value="提交"></td>
        </tr>
    </table>
</form>
</body>
</html>
